package UI;

import Logic.Poker;
import Logic.PokerGame3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class GameJFrame extends JFrame implements MouseListener {
    public static Container container=null;
    //抢地主 不抢
    JButton []landlord=new JButton[2];
    //出牌 不要
    JButton []publishCard=new JButton[2];

    JLabel diZhu;

    //集合嵌套集合 大集合中三个小集合 表示出过的牌
    ArrayList<ArrayList<Poker>> list=new ArrayList<>();

    ArrayList<ArrayList<Poker>> player=new ArrayList<>(); //手里的牌

    //底牌
    ArrayList<Poker> lord=new ArrayList<>();

    //盒装牌
    ArrayList<Poker> pokerList=new ArrayList<>();

    //计时框
    JTextField []time=new JTextField[3];

    public GameJFrame(){
        setIconImage(Toolkit.getDefaultToolkit().getImage("src\\Image\\Poker\\dizhu.png"));
        InitJFrame();

        InitCard();
        InitImage();



        this.setVisible(true);

    }

    public void InitJFrame(){

        this.setSize(830,620);
        this.setTitle("斗地主单机版v1.0");
        this.setDefaultCloseOperation(3);
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setLayout(null);

        this.setResizable(false);//窗口无法调节

        container=this.getContentPane();
        container.setLayout(null);
        container.setBackground(Color.LIGHT_GRAY);

    }
    public void InitImage(){
        JButton rob=new JButton("抢地主");
        rob.setBounds(320,400,75,20);
        rob.addMouseListener(this);
        rob.setVisible(false);
        landlord[0]=rob;
        container.add(rob);

        JButton noRob=new JButton("不  抢");
        noRob.setBounds(420,400,75,20);
        noRob.addMouseListener(this);
        noRob.setVisible(false);
        landlord[1]=noRob;
        container.add(noRob);

        JButton outCard=new JButton("出牌");
        outCard.setBounds(320,400,60,20);
        outCard.addMouseListener(this);
        outCard.setVisible(false);
        publishCard[0]=outCard;
        container.add(outCard);

        JButton NoCard=new JButton("不出");
        NoCard.setBounds(420,400,60,20);
        NoCard.addMouseListener(this);
        NoCard.setVisible(false);
        publishCard[1]=NoCard;
        container.add(NoCard);

        for(int i=0;i<3;i++){
            time[i]=new JTextField("倒计时：");

            time[i].setEditable(false);
            time[i].setVisible(false);
            container.add(time[i]);
        }
        time[0].setBounds(140,230,60,20);
        time[1].setBounds(374,360,60,20);
        time[2].setBounds(620,230,60,20);

        //创建图标
        diZhu=new JLabel(new ImageIcon("src\\Image\\Poker\\dizhu.png"));
        diZhu.setVisible(false);
        diZhu.setSize(40,40);

        container.add(diZhu);
    }

    public void InitCard(){
        for(int i=1;i<=5;i++){
            for(int j=1;j<=13;j++){
                if((i==5)&&j>2){
                    break;
                }else{
                    Poker poker=new Poker(i+"-"+j,false);
                    poker.setLocation(350,150);
                    pokerList.add(poker);
                    container.add(poker);
                }
            }
        }

        Collections.shuffle(pokerList);

        ArrayList<Poker> player0=new ArrayList<>();
        ArrayList<Poker> player1=new ArrayList<>();
        ArrayList<Poker> player2=new ArrayList<>();

        for(int i=0;i<pokerList.size();i++){
            Poker poker=pokerList.get(i);

            if(i>=list.size()-3){
                lord.add(poker);
                Common.move(poker,poker.getLocation(),new Point(270+(75*i),10));
                continue;
            }
            else {
                if (i % 3 == 0) {
                    Common.move(poker,poker.getLocation(),new Point(50,60+i*5));
                    player0.add(poker);
                } else if (i % 3 == 1) {
                    Common.move(poker,poker.getLocation(),new Point(180+i*7,450));
                    player1.add(poker);
                } else {
                    Common.move(poker,poker.getLocation(),new Point(700,60+i*5));
                    player2.add(poker);
                }
            }

            player.add(player0);
            player.add(player1);
            player.add(player2);

            container.setComponentZOrder(poker,0);
        }

        for(int i=0;i<3;i++){
            sort(player.get(i));
            Common.rePosition(this,player.get(i),i);
        }


    }


    public static void sort(ArrayList<Poker> list){

    Collections.sort(list, new Comparator<Poker>() {
    @Override
    public int compare(Poker o1, Poker o2) {
        int color1=Integer.parseInt(o1.getName().substring(0,1));
        int value1=getValue(o1);

        int color2=Integer.parseInt(o2.getName().substring(0,1));
        int value2=getValue(o2);

        int flag=value2-value1;
        if(flag==0){
            return color2-color1;
        }else{
            return flag;
        }

    }
});

    }
    public static int getValue(Poker o1){
        String name=o1.getName();
        int color=Integer.parseInt(name.substring(0,1));
        int value=Integer.parseInt(name.substring(2));
        if(color==5){
            return value+=100;
        }

        if(value==1){
            return value+=20;
        }
        if(value==2){
            return value+=30;
        }
        return value;

    }
    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
